package com.abbas.wallerpaper.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.abbas.wallerpaper.R;
import com.bumptech.glide.Glide;

import java.util.List;

public class FlowerAdapter extends PagerAdapter {

    private List<Integer> mData;

    private Context mContext;

    @Override
    public int getCount() {
        return mData.size();// 返回数据的个数
    }

    public FlowerAdapter(Context ctx, List<Integer> data) {
        this.mContext = ctx;
        this.mData = data;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;// 过滤和缓存的作用

    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = View.inflate(container.getContext(), R.layout.result_flower_item, null);

        ImageView imageView = view.findViewById(R.id.iv_photo);
        Glide.with(mContext).load(mData.get(position)).into(imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext, "当前条目：" + position, Toast.LENGTH_SHORT).show();
            }

        });
        container.addView(view);//添加到父控件

        return view;
    }
}
